<?php
$title                  = get_input('title');
$description            = get_input('description');
if(!$description) {
	$description = $title;
}
$user_id                = get_input('userid');
if(!$user_id)
    $user_id            = get_loggedin_userid();
$shared                 = 0;
$width                  = 640;
$height                 = 960;
$orientation            = 1;
$device_model           = get_input('device_model');
$device_uid             = get_input('device_uuid');
$lat                    = get_input('lat');
$lng                    = get_input('lng');
$location_description   = '';
$md5                    = get_input('md5');

if($md5) {
    $cdn = file_get_contents("http://www.whiteboardgallery.com/CDN/md5.php?md5=$md5");
    $cdn = json_decode($cdn, TRUE);
    if($cdn && $cdn['status'] == 200) {
        $url            = $cdn['url'];
        $thumbnail_url  = $cdn['thumbnail_url'];  
    } else {
        
    }
} else {
    $url                = get_input('url');
    if($url) {
        echo '-';
        $len = strlen($url);
        $len1 = strlen("http://www.whiteboardgallery.com/uploads/");
        $len2 = strlen(".png");
        $did = substr($url, $len1, $len - $len2 - $len1);
        echo $did;
        if(is_numeric($did)) {
            $cdn = file_get_contents("http://www.whiteboardgallery.com/CDN/id.php?id=$did");
            print_r($cdn);
            $cdn = json_decode($cdn, TRUE);
            print_r($cdn);
            if($cdn && $cdn['status'] == 200) {
                $url            = $cdn['url'];
                $thumbnail_url  = $cdn['thumbnail_url'];  
                $md5            = $cdn['md5'];
            } else {

            }
        } else {
            $md5 = $did;
        } 
    }
    // $thumbnail_url      = get_input('thumbnail_url');
    if(!$thumbnail_url) {
        $thumbnail_url = $url;
    }
}
$drawing_id = add_drawing($title, $description, $user_id, $shared, 
                        $width, $height, $orientation, 
                        $device_model, $device_uid, 
                        $md5, $url, $thumbnail_url, 
                        $lat, $lng, $location_description);

if($drawing_id) {
    $collaborator_ids   = get_input('collaborator_ids');
    foreach($collaborator_ids as $collaborator_id) {
        add_drawing_collaborators($collaborator_id, $drawing_id, 1);
    }
    $tags               = explode(',',get_input('tags'));
    add_drawing_tags($drawing_id, $tags);
    echo json_encode(export_result(200, "201 Created"));
} else {
    echo json_encode(export_result(400, "400 Bad Request, $md5, $title, $url"));
}

?>